(function() {
    'use strict';

    /**
     * @ngdoc function
     * @name webApp.controller:MaterialCtrl
     * @description
     * # MaterialCtrl
     * Controller of the webApp
     */
    angular.module('webApp')
        .controller('MaterialCtrl', MaterialCtrl);

    MaterialCtrl.$inject = ['$scope', 'dataService', '$timeout', '$location', 'helper', '$document', '$filter', 'sessionService'];


    function MaterialCtrl($scope, dataService, $timeout, $location, helper, $document, $filter, sessionService) {


        //初始方法
        $scope.listAction = function() {
            $scope.pageSize = 4;
            $scope.$watch('wxappid', function(newValue, oldValue, scope) {
                if (newValue) {
                    $scope.wxappid = newValue;
                    $scope.getlist(1);
                }
            }, true);
        }

        //获取列表
        $scope.getlist = function(pageNo) {
            $scope.pageNo = pageNo ? pageNo : $scope.pageNo;
            // var query = {};
            var query = { page_no: $scope.pageNo, page_size: $scope.pageSize };
            if ($scope.wxappid) {
                query.query = { "app_id": $scope.wxappid };
            }
            return dataService.materialGet(query).then(function(list) {
                $scope.attachs = list.materialRecords;
                console.log($scope.attachs)
                $scope.attachsTotal = list.total_count;
            });
        }



        $scope.addAction = function() {
            $scope.materail_rec_id = $location.search().materail_rec_id;
            // console.log("materail_rec_id",$scope.materail_rec_id);
            if ($scope.materail_rec_id) {
                $scope.mtoken = sessionService.get('token');
                return dataService.materialGet({ query: { "materail_rec_id": $scope.materail_rec_id } }).then(function(list) {
                    $scope.materail = list.materialRecords[0];
                });
            } else {

            }
        }


    }
})();